package com.qrcode.scan;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;

import androidx.lifecycle.LifecycleOwner;

import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;

/**
 * 项目名称：com.qrcode.scan
 * 创建人:LuoFG
 * 创建时间:2024/8/26  17:46
 * 描述:
 * 作用:
 */
public class QRClient {
    private static class InstanceHolder {
        private static QRClient instance = new QRClient();
    }

    private static QRClient getInstance() {
        return QRClient.InstanceHolder.instance;
    }

    public static QRClient getClient() {
        return getInstance();
    }

    /***配置资源-Start***/
    //相册功能是否开放
    private boolean isOpenPhoto = false;
    //默认Bar返回键
    private int imgBack = R.mipmap.failed;

    //是否自动缩放
    private boolean isAutoZoom = false;
    //是否是条形码框 true为条形码
    private boolean isBarcode = false;
    //扫描框边框默认颜色  白色
    private int scanBorderColor = Color.WHITE;
    //扫描框边角线默认颜色（折角）  白色
    private int scanCornerColor = Color.WHITE;
    //扫描线默认颜色  白色
    private int scanLineColor = Color.WHITE;
    //扫描提示默认颜色  白色
    private int scanTipTextColor = Color.WHITE;
    //扫描提示
    private String scanTipText = "将二维码/条码放入框内，即可自动扫描";
    /***配置资源-End***/
    //传入具体包名类  作为区分
    private String ScanTag="";
    //标题栏颜色是否沉浸到状态栏
    private boolean isToOsBar = false;
    //是否显示右上角  闪光灯按钮
    private boolean isShowRightPower=false;
    //巡检监听
    private QRClient.inspectionOnClickListen inspectionOnClickListen=null;
    //相册监听
    private QRClient.photoOnClickListen photoOnClickListen=null;
    //水印文字
    private String strWaterMark="";
    //相册选择后回调路径给框架
    private PhotoResult photoResult=null;
    //拍照或者选择相册后确定图片的回调
    private PhotoResult takePhotoResult=null;
    //拍照重置
    private CameraReset cameraReset=null;
    //QR类型
    private QRType qrType=QRType.ALL;

    public enum QRType{
        ALL,
        CAMERA,
        SCAN
    }

    //设置成为默认属性
    public QRClient setDefault() {
        //相册功能是否开放
        isOpenPhoto = false;
        //是否自动缩放
        isAutoZoom = true;
        //默认Bar返回键
        imgBack = R.mipmap.failed;

        //是否是条形码框 true为条形码
        isAutoZoom = false;
        //是否是条形码框 true为条形码
        isBarcode = false;
        //扫描框边框默认颜色  白色
        scanBorderColor = Color.WHITE;
        //扫描框边角线默认颜色（折角）  白色
        scanCornerColor = Color.WHITE;
        //扫描线默认颜色  白色
        scanLineColor = Color.WHITE;
        //扫描提示默认颜色  白色
        scanTipTextColor = Color.WHITE;
        //扫描提示
        scanTipText = "将二维码/条码放入框内，即可自动扫描";
        isToOsBar=false;
        isShowRightPower=false;
        inspectionOnClickListen=null;
        photoOnClickListen=null;
        strWaterMark="";
        photoResult=null;
        takePhotoResult=null;
        cameraReset=null;
        qrType=QRType.ALL;
        return this;
    }

    public QRType getQrType() {
        return qrType;
    }

    public QRClient setQrType(QRType qrType) {
        this.qrType = qrType;
        return  this;
    }

    public interface CameraReset{
        void back();
    }

    public CameraReset getCameraReset() {
        return cameraReset;
    }

    public QRClient setCameraReset(CameraReset cameraReset) {
        this.cameraReset = cameraReset;
        return  this;
    }

    public interface ScanResult{
        void back(QRCodeResultBean result);
    }

    public interface inspectionOnClickListen{
        void onClick();
    }

    public interface photoOnClickListen{
        void onClick(Activity activity);
    }

    public interface PhotoResult{
        void back(String path);
    }

    public PhotoResult getTakePhotoResult() {
        return takePhotoResult;
    }

    public void setTakePhotoResult(PhotoResult takePhotoResult) {
        this.takePhotoResult = takePhotoResult;
    }

    public QRClient setScanResultListener(Activity activity, QRClient.ScanResult scanResult){
        try {
            QRActivity.qrcodeScanResult.observe((LifecycleOwner) activity, s -> {
                //Log.e("收到的识别回调",s.getTag()+s.getResult());
                scanResult.back(s);
                QRActivity.qrcodeScanResult.removeObservers((LifecycleOwner) activity);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public String getStrWaterMark() {
        return strWaterMark;
    }

    public QRClient setStrWaterMark(String strWaterMark) {
        this.strWaterMark = strWaterMark;
        return this;
    }

    public PhotoResult getPhotoResult() {
        return photoResult;
    }

    public QRClient setPhotoResult(PhotoResult photoResult) {
        this.photoResult = photoResult;
        return this;
    }

    /**
     * @param activity 启动QR识别和拍照
     */
    public void startQR(Activity activity){
        activity.startActivity(new Intent(activity, QRActivity.class));
    }

    public QRClient setDebug(boolean debug){
        BGAQRCodeUtil.setDebug(debug);
        return this;
    }

    public QRClient setToOsBar(boolean toOsBar){
        isToOsBar=toOsBar;
        return this;
    }

    public boolean isToOsBar() {
        return isToOsBar;
    }

    public String getScanTag() {
        return ScanTag;
    }

    public QRClient setScanTag(String ScanTag) {
        this.ScanTag = ScanTag;
        return this;
    }

    public boolean isAutoZoom() {
        return isAutoZoom;
    }

    public QRClient setAutoZoom(boolean autoZoom) {
        isAutoZoom = autoZoom;
        return this;
    }

    public boolean isBarcode() {
        return isBarcode;
    }

    public QRClient setBarcode(boolean barcode) {
        isBarcode = barcode;
        return this;
    }

    public int getScanCornerColor() {
        return scanCornerColor;
    }

    /**
     * @param scanCornerColor  此方法暂时不生效
     * @return
     */
    public QRClient setScanCornerColor(int scanCornerColor) {
        this.scanCornerColor = scanCornerColor;
        return this;
    }

    public boolean isOpenPhoto() {
        return isOpenPhoto;
    }

    public QRClient setOpenPhoto(boolean openPhoto) {
        isOpenPhoto = openPhoto;
        return this;
    }

    public int getImgBack() {
        return imgBack;
    }

    public QRClient setImgBack(int imgBack) {
        this.imgBack = imgBack;
        return this;
    }

    public int getScanBorderColor() {
        return scanBorderColor;
    }

    public QRClient setScanBorderColor(int scanBorderColor) {
        this.scanBorderColor = scanBorderColor;
        return this;
    }

    public int getScanLineColor() {
        return scanLineColor;
    }

    public QRClient setScanLineColor(int scanLineColor) {
        this.scanLineColor = scanLineColor;
        return this;
    }

    public int getScanTipTextColor() {
        return scanTipTextColor;
    }

    public QRClient setScanTipTextColor(int scanTipTextColor) {
        this.scanTipTextColor = scanTipTextColor;
        return this;
    }

    public String getScanTipText() {
        return scanTipText;
    }

    public QRClient setScanTipText(String scanTipText) {
        this.scanTipText = scanTipText;
        return this;
    }

    public boolean isShowRightPower() {
        return isShowRightPower;
    }

    public QRClient setShowRightPower(boolean showRightPower) {
        isShowRightPower = showRightPower;
        return this;
    }

    public QRClient.inspectionOnClickListen getInspectionOnClickListen() {
        return inspectionOnClickListen;
    }

    public QRClient setInspectionOnClickListen(QRClient.inspectionOnClickListen inspectionOnClickListen) {
        this.inspectionOnClickListen = inspectionOnClickListen;
        return this;
    }

    public QRClient.photoOnClickListen getPhotoOnClickListen() {
        return photoOnClickListen;
    }

    public QRClient setPhotoOnClickListen(QRClient.photoOnClickListen photoOnClickListen) {
        this.photoOnClickListen = photoOnClickListen;
        return this;
    }
}
